// (c) Copyright Mikhail Dikov. All other rights reserved.
// (c) Copyright Rajkumar Chityal. All other rights reserved.
/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />
/// <reference path="../ExtenderBase/BaseScripts.js" />
/// <reference path="../Compat/Timer/Timer.js" />
/// <reference path="../Common/Common.js" />
/// <reference path="../Animation/Animations.js" />
   
Type.registerNamespace('WebPartExtenders');

WebPartExtenders.SplitterWebPartExtender = function(element)
{
    WebPartExtenders.SplitterWebPartExtender.initializeBase(this, [element]);
    
    this._leftZoneID = null;
    this._rightZoneID = null;
    
    //private
    this._splitterDiv = null;
    this._leftZonePanel = null;
    this._rightZonePanel = null;
   
    this._currentWidth = 0; 
    this._currentX = 0; 
    this._newX = 0; 
    this._mouseButtonPos = "up";  
    this._prevTdWidth="";
    
    
    // Event handlers
    this._setPositionHandler = null;  
    this._toggleZoneHandler = null;    
    this._mouseMoveHandler = null;    
    this._mouseUpHandler = null;    
      
     
}
   
WebPartExtenders.SplitterWebPartExtender.prototype = {

    initialize : function() {
    WebPartExtenders.SplitterWebPartExtender.callBaseMethod(this, 'initialize');
       var element = this.get_element();
        if (!element) throw Error.invalidOperation("Element required");
      
        this._setPositionHandler = Function.createDelegate(this, this.setPosition);
        this._toggleZoneHandler =  Function.createDelegate(this, this.toggleZone);
        this._mouseMoveHandler  =  Function.createDelegate(this, this.getPosition);
        this._mouseUpHandler    =  Function.createDelegate(this, this.setMouseUp);
        
        //this._rightZonePanel = $get(this._rightZoneID);
        this._leftZonePanel = element;
        this._rightZonePanel = this._leftZonePanel.parentElement.nextSibling.firstChild;
        
        this.createSplitterDiv(this._leftZonePanel ,this._rightZonePanel);         

        // Find the web part manager table
        
        // inject a div or TD in the middle
        
        // draw splitter
        
        // add handlers
    
    },


//    get_leftZoneID : function() { return this._leftZoneID; },
//    set_leftZoneID : function(value) { this._leftZoneID = value; },
//    
    get_RightZoneID : function() { return this._rightZoneID; },
    set_RightZoneID : function(value) { this._rightZoneID = value; },
    
    dispose : function() {

        // Remove the event handler from the element if attached.
        
        if(this._setPositionHandler) {
            $removeHandler(this._splitterDiv, 'mousedown', this._setPositionHandler);
            this._setPositionHandler = null;
        }

        if(this._toggleZoneHandler) {
            $removeHandler(this._splitterDiv, 'dblclick', this._toggleZoneHandler);
            this._toggleZoneHandler = null;
        }
        if(this._mouseMoveHandler) {
            $removeHandler(document.body, 'mousemove', this._mouseMoveHandler);
            this._mouseMoveHandler = null;
        }
        if(this._mouseUpHandler) {
            $removeHandler(document.body, 'mouseup', this._mouseUpHandler);
            this._mouseUpHandler = null;
        }
        WebPartExtenders.SplitterWebPartExtender.callBaseMethod(this, 'dispose');
    },
    
    setPosition: function(e)
    {
        //For handling events in ie vs. w3c. 
        curEvent = ((typeof event == "undefined")? e: event); 
        //Sets mouse flag as down. 
        this._mouseButtonPos = "down"; 
        //Gets position of click. 
        this._currentX = curEvent.clientX; 
        
        //Get the width of the div. 	
        var tempWidth = this._leftZonePanel.parentElement.style.width;
        if(tempWidth == "")
        {
            this._currentWidth  = this._leftZonePanel.parentElement.offsetWidth;
        }
        else
        {
            //Get the width value. 
            var widthArray = tempWidth.split("p"); 
            //Set the current width. 
            this._currentWidth = parseInt(widthArray[0]); 
        }
        
    },
    getPosition:function (e)
    { 
        if( this._mouseButtonPos == "down" ) 
        { 
            //For handling events in ie vs. w3c. 
            curEvent = ((typeof event == "undefined")? e: event); 
            //Get new mouse position. 
            this._newX = curEvent.clientX; 
            //Calculate movement in pixels. 
            var pixelMovement = parseInt(this._newX - this._currentX); 
            //Determine new width. 
            var newWidth = parseInt(this._currentWidth + pixelMovement); 
            //Enforce a minimum width. 
            newWidth = ((newWidth < 150)? 150: newWidth); 
            //Set the new width. 
	    
	       //SetDivZoneTdWidthTo100Percent(g_rightFrameDivId);
  	       //document.getElementById(g_leftFrameDivId).style.overflow="auto";
            
            this._leftZonePanel.parentElement.style.width = newWidth + "px"; 
            //Set the new left of the splitter bar. 
            this._leftZonePanel.parentElement.style.left = parseInt(this._leftZonePanel.parentElement.style.width) + 10; 
            
        }         
    },
    setMouseUp:function ()
	{
		this._mouseButtonPos='up';		
	},

    toggleZone: function()
    {     
        if(this._leftZonePanel.style.display != "none")
        {
            this._leftZonePanel.style.display = "none";
            this._prevTdWidth = this._leftZonePanel.parentElement.style.width;
            this._leftZonePanel.parentElement.style.width = "";
        }
        else
        {
            this._leftZonePanel.style.display = "";
            this._leftZonePanel.parentElement.style.width  = this._prevTdWidth;
            this._prevTdWidth = "";
        }
	},
	
    createSplitterDiv: function ()
    {   
        var splitTd = document.createElement('td');    
        this._splitterDiv = document.createElement('div');
        this._splitterDiv.style.height = this._leftZonePanel.clientHeight.toString() + 'px';
        
        var attrClass = document.createAttribute('class');
        attrClass.value = 'SplitterWebPartExtender';
        this._splitterDiv.attributes.setNamedItem(attrClass);
        
        $addHandler(this._splitterDiv, "mousedown" , this._setPositionHandler);
        $addHandler(this._splitterDiv, "dblclick" , this._toggleZoneHandler);        
        
        
        $addHandler(document.body, "mousemove" , this._mouseMoveHandler);        
        $addHandler(document.body, "mouseup" , this._mouseUpHandler);        
        
        //window.document.body.onmousemove=getPos
        splitTd.appendChild(this._splitterDiv);
        this._rightZonePanel.parentElement.parentElement.insertBefore(splitTd, this._rightZonePanel.parentElement);                
        // testing required.
        
    }
}

WebPartExtenders.SplitterWebPartExtender.registerClass('WebPartExtenders.SplitterWebPartExtender', AjaxControlToolkit.BehaviorBase);   





//    //Function getPos(...) changes the width of the div while the mouse button is pressed. 
   

//SetDivStates:function ()
//{
//    if(document.getElementById(g_leftFrameDivId) == null || document.getElementById(g_rightFrameDivId))
//	{
//	    return;
//	}
//	var args = SetDivStates.arguments;

//	for (var i = 0; i < args.length; i++) 
//	{
//		controlObj = document.getElementById(args[i]);
//		if(controlObj != null)
//		{	
//			do
//			{
//				if(controlObj.tagName == 'BODY')
//				{
//					break;
//				}
//				if(controlObj.tagName == 'TD')
//				{
//					
//					if(controlObj.id == '_invisibleIfEmpty')
//					{
//						var divCookie = GetCookie('ToggleDivs'+args[i]); 
//						if(divCookie == "visible")
//	    					{							
//		    					
//							controlObj.style.display = "";
//	    					}
//	    					else
//	    					{		
//							controlObj.style.display = "none";
//							
//	    					}						
//					}
//				}
//				controlObj = controlObj.parentElement;

//			}while(true);
//		}
//	}

//}
//
